module namespace search = "http://www.xmlproject.com/smi/lib/controller/searchmovies";

import module namespace rest = "http://expath.org/ns/http-client";
import module namespace json = "http://www.zorba-xquery.com/modules/json";
import module namespace base64 = "http://www.zorba-xquery.com/modules/base64";
import module namespace html = "http://www.zorba-xquery.com/modules/tidy";
import module namespace util = "http://www.zorba-xquery.com/zorba/util-functions";


declare sequential function search:imdb($query as xs:string, $year as xs:string)
{
    (: prepare the search url :)
    let $searchstring := fn:encode-for-uri($query)
    let $imdbUrl := 
        if($year = "" or $year = "- Year -")
        then fn:concat("http://www.imdbapi.com/?t=", $searchstring, "&amp;r=XML&amp;tomatoes=true&amp;plot=full")
        else fn:concat("http://www.imdbapi.com/?t=", $searchstring, "&amp;r=XML&amp;y=", $year, "&amp;tomatoes=true&amp;plot=full")
                    
    (: get the results :)
    return
        <searchresult>
            <provider>IMDB</provider>
            {
                fn:trace($imdbUrl, '#search:imdb: The value of $imdbUrl is: '),
                (: send request to google json api and parse the response :)
                let $hits := rest:send-request((), $imdbUrl)[2]
                for $hit in $hits//movie
                let $title := string($hit/@title)
                let $year := string($hit/@year)                
                let $director := string($hit/@director)
                let $genre := string($hit/@genre)
                let $imdbid := string($hit/@id)
                let $plot := concat("&lt;p>",string($hit/@plot),"&lt;/p>")
                
                (: we use tidy to remove the encoded html tags (e.g. &lt;) in the google contents :)
                let $formattedPlot := html:tidy($plot)  
                
                return
                    <movie imdbid="{$imdbid}">
                        <title>{$title}</title>                        
                        <year>{$year}</year>
                        <plot>{data($formattedPlot)}</plot>
                        <specialinfo>
                            <director>{$director}</director>
                            <genre>{$genre}</genre>
                        </specialinfo>                            
                    </movie>                    
            }
        </searchresult>
};

declare sequential function search:themoviedb($query as xs:string, $year as xs:string)
{
    (: prepare the search url :)
    let $searchstring := fn:encode-for-uri($query)
    let $theMovieDbUrl := fn:concat("http://api.themoviedb.org/2.1/Movie.search/en/xml/3b95274f2f660c999668adef7b824dd8/", $searchstring)
    
    let $theMovieDbUrl := 
        if($year = "" or $year = "- Year -")
        then fn:concat("http://api.themoviedb.org/2.1/Movie.search/en/xml/3b95274f2f660c999668adef7b824dd8/", $searchstring)
        else fn:concat("http://api.themoviedb.org/2.1/Movie.search/en/xml/3b95274f2f660c999668adef7b824dd8/", $searchstring, "+", $year)
        
    (: get the results :)
    return
        <searchresult>
            <provider>The Movie DB</provider>
            {
                let $hits := rest:send-request((), $theMovieDbUrl)[2]
                for $hit in $hits//movie               
                let $title := $hit/original_name/text()
                let $year := fn:substring($hit/released/text(), 1, 4)
                let $tmdbid := $hit/id/text()
                let $imdbid := $hit/imdb_id/text()
                let $rating := $hit/rating/text()         
                let $plot := concat("&lt;p>",$hit/overview/text(),"&lt;/p>")
                
                (: we use tidy to remove the encoded html tags (e.g. &lt;) in the google contents :)
                let $formattedPlot := html:tidy($plot)
                
                return
                    <movie tmdbid="{string($tmdbid)}" imdbid="{string($imdbid)}">
                        <title>{$title}</title>                        
                        <year>{$year}</year>
                        <plot>{data($formattedPlot)}</plot>
                        <specialinfo>
                            <rating>{$rating}</rating>
                        </specialinfo> 
                    </movie>
            }
        </searchresult>
};

declare sequential function search:rTomatoes($query as xs:string, $year as xs:string)
{
    (: prepare the search url :)
    let $searchstring := fn:encode-for-uri($query)
    let $rTomatoesUrl := fn:concat("http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=xakjcx3ztntapmd3fptsrtbs&amp;q=", $searchstring, "&amp;page_limit=10")
        
    (: get the results :)
    return
        <searchresult>
            <provider>Rotten Tomatoes</provider>
            {          
                (: send request to google json api and parse the response :)
                let $hits_json := rest:send-request((), $rTomatoesUrl)[2] 
                let $hits_xml := json:parse($hits_json)
                
                (: for each hit create a result element :)
                for $hit in $hits_xml//pair[@name="movies"]/item
                let $rtid := $hit/pair[@name="id"]/text()
                let $title := $hit/pair[@name="title"]/text()
                let $year := $hit/pair[@name="year"]/text()
                let $url := $hit/pair[@name="links"]/pair[@name="alternate"]/text()
                let $rating := $hit/pair[@name="ratings"]/pair[@name="critics_rating"]/text()
                let $plot := concat("&lt;p>",$hit/pair[@name="synopsis"]/text(),"&lt;/p>")
                
                (: we use tidy to remove the encoded html tags (e.g. &lt;) in the google contents :)
                let $formattedPlot := html:tidy($plot)
                
                return
                    <movie rtid="{string($rtid)}">
                        <title>{$title}</title>                  
                        <year>{$year}</year>
                        <plot>{data($formattedPlot)}</plot>
                        <specialinfo>
                            <rating>{$rating}</rating>
                        </specialinfo> 
                    </movie>
            }
        </searchresult>
};



 
declare sequential function search:ebay($query as xs:string)
{
    let $searchstring := fn:encode-for-uri($query)
    let $ebayurl_part1 := "http://open.api.ebay.com/shopping?callname=FindPopularItems&amp;appid=AnthonyA-e5d4-4569-b305-0cc685ea12d3&amp;responseencoding=XML&amp;siteid=77&amp;QueryKeywords="
    let $ebayurl_part2 := "&amp;AvailableItemsOnly=true&amp;MaxEntries=10&amp;version=745"
    let $ebayurl_part3 := fn:concat($ebayurl_part1, $searchstring)
    let $ebayurl := fn:concat($ebayurl_part3, $ebayurl_part2)
            
    (: get the results :)
    let $searchstring := $query
    
    return
        <searchresult>
            <provider>eBay</provider>
            {
                let $hits := rest:send-request((), $ebayurl)[2]
                for $hit in $hits//Item
                let $title := $hit/Title/text()
                let $url := $hit/ViewItemURLForNaturalSearch
                let $price := concat("&lt;p>",$hit/ConvertedCurrentPrice/text(),"&lt;/p>")
                
                (: we use tidy to remove the encoded html tags (e.g. &lt;) in the google contents :)
                let $content := html:tidy($price)
                
                return <result title="{$title}" href="{$url}">{$content}</result>
            }
        </searchresult>
};



